package com.hzlh.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hzlh.common.util.BaseConstants;
import com.hzlh.domain.po.IPhoneStartPo;
import com.hzlh.domain.po.LoginAdmin;
import com.hzlh.domain.po.PhoneUserPo;
import com.hzlh.mapper.LoginMapper;

@Service
public class UserService {
	@Autowired
	private LoginMapper mapper;
	
	public LoginAdmin login(LoginAdmin user){
		return mapper.login(user);
	}
	
	public List<Map<String, String>> getByIdOrPhone(Map<String, Object> map){
		return mapper.getByIdOrPhone(map);
	}
	
	public Map<String,Object> userPage(int weight,int pageIndex,int pageSize){
		Map<String, Object> remap = new HashMap<String,Object>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("weight", weight);
		map.put("start", pageIndex);
		map.put("end", pageSize);
		int count = mapper.getUserCount(map);
		List<LoginAdmin> list = null;
		if(count>0){
			list = mapper.getUserPage(map);
		}
		remap.put(BaseConstants.MCPP_HC_TOTAL_RECORDS, count);
		remap.put(BaseConstants.MCPP_HC_PAGE_DATA, list);
		return remap;
	}
	
	public Map<String, Object> userLoginPage(String startDate, String endDate ,String phone, String name,
			int pageIndex, int pageSize) {
		Map<String, Object> remap = new HashMap<String,Object>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("start", pageIndex);
		map.put("end", pageSize);
		map.put("LoginStartDate", startDate);
		map.put("LoginEndDate", endDate);
		map.put("activeUser", PhoneUserPo.LOGIN_ACTIVE_USER);
		map.put("phone", phone);
		map.put("name", name);
		int count = mapper.getPhoneUserCount(map);
		List<PhoneUserPo> list = null;
		if(count>0){
			list = mapper.getPhoneUserPage(map);
		}
		remap.put(BaseConstants.MCPP_HC_TOTAL_RECORDS, count);
		remap.put(BaseConstants.MCPP_HC_PAGE_DATA, list);
		return remap;
	}
	
	public Map<String, Object> userActivePage(String startDate, String endDate ,String nameType,
			int pageIndex, int pageSize) {
		Map<String, Object> remap = new HashMap<String,Object>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("start", pageIndex);
		map.put("end", pageSize);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		if(nameType == null || "".equals(nameType.trim())){
			map.put("nameType", null);
		}else{
			if("安卓".equals(nameType)){
				map.put("nameType", 1);
			}else
			if("IOS".equalsIgnoreCase(nameType.trim())){
				map.put("nameType", "0");
			}else{
				map.put("nameType", -1);
			}
		
		}
		int count = mapper.getPhoneActiveCount(map);
		List<IPhoneStartPo> list = null;
		list = mapper.getPhoneActivePage(map);
		if(list.size() > 0){
			
			for (int i = 0; i < list.size(); i++) {
				String lists = list.get(i).getUuId();
				if(null == lists || "".equals(lists)){
					list.get(i).setUuId("--");
				}else{
					String uuid ="****" +  lists.substring(3, 7)
							+ "****" ;
					list.get(i).setUuId(uuid);
				}
				
			}
		}
//		int count = list.size();
		remap.put(BaseConstants.MCPP_HC_TOTAL_RECORDS, count);
		remap.put(BaseConstants.MCPP_HC_PAGE_DATA, list);
		return remap;
	}
	
	
	public Map<String, Object> userRegisterPage(String startDate, String endDate ,String phone, String name,
			int pageIndex, int pageSize) {
		Map<String, Object> remap = new HashMap<String,Object>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("start", pageIndex);
		map.put("end", pageSize);
		map.put("RegStartDate", startDate);
		map.put("RegEndDate", endDate);
		map.put("phone", phone);
		map.put("name", name);
		int count = mapper.getPhoneUserCount(map);
		List<PhoneUserPo> list = null;
		if(count>0){
			list = mapper.getPhoneUserPage(map);
		}
		remap.put(BaseConstants.MCPP_HC_TOTAL_RECORDS, count);
		remap.put(BaseConstants.MCPP_HC_PAGE_DATA, list);
		return remap;
	}
	
	public int isUserExist(String userId,Long id){
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("filter_id", id);
		if(StringUtils.isNotEmpty(userId)){
			paramMap.put("isUserId", userId);
		}
		return mapper.getUserCount(paramMap);		
	}
	
	public int addUser(LoginAdmin po){
		return mapper.insertUser(po);
	}
	
	public int editUser(LoginAdmin po){
		return mapper.updateUser(po);
	}
	
	public LoginAdmin getUser(Long id){
		return mapper.getUserById(id);
	}
	
	public boolean deleteUser(Long[] ids){
		return mapper.deleteUser(ids) > 0;
	}

	public List<LoginAdmin> getPresenterList(Map<String, Object> map) {
		return mapper.getPresenterList(map);
	}
	
	public List<LoginAdmin> getResourcePresenterList(Map<String, Object> map) {
		return mapper.getResourcePresenterList(map);
	}
	
	public List<LoginAdmin> getResourcePresenterList(Integer type , Long resourceId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", type);
		map.put("resourceId", resourceId);
		return mapper.getResourcePresenterList(map);
	}

	public Long[] getResourcePresenterIds(int type, Long resourceId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", type);
		map.put("resourceId", resourceId);
		return mapper.getResourcePresenterIds(map);
	}
	
}
